package com.gitee.qdbp.general.common.biz.event.service;

import java.util.List;
import com.gitee.qdbp.general.common.api.event.model.IListenerBean;
import com.gitee.qdbp.general.common.api.event.service.IEventRegister;
import com.gitee.qdbp.tools.utils.VerifyTools;

public class EventRegister implements IEventRegister {

    private EventCenter center;

    public EventRegister(EventCenter center) {
        this.center = center;
    }

    public void setListeners(List<IListenerBean<?, ?, ?>> beans) {
        center.clear();
        for (IListenerBean<?, ?, ?> bean : beans) {
            if (VerifyTools.isBlank(bean.getDataType())) {
                String bcn = bean.getClass().getSimpleName();
                throw new IllegalArgumentException(bcn + ".dataType must be not null.");
            }
            this.add(bean);
        }
    }

    @Override
    public void add(IListenerBean<?, ?, ?> bean) {
        center.addListener(bean.getDataType(), bean);
    }

    @Override
    public void remove(IListenerBean<?, ?, ?> bean) {
        center.removeListener(bean.getDataType(), bean);
    }
}
